<?php
include_once "Item_DAO.php";
include_once "Itemtype.php";
include_once "Itemstatus.php";
include_once "Activity.php";
include_once "User.php";

class Item extends Item_DAO{	
	const ACTION_TYPE=0;
	const REPORTITEM_TYPE=1;
	
	const ACTION_STATUS_OPENED=0;
	const ACTION_STATUS_CLOSED=1;
	const REPORTITEM_STATUS_CREATED=2;
	
	/**
	 * An action can only be displayed by:
	 * <ul>
	 * <li>The user who created the action</li>
	 * <li>The user who has been allocated with the action</li>
	 * <li>The manager (direct or indirect) of the user who has been allocated with the action</li>
	 * </ul>
	 */
	public function isAccessAllowed($p_userId){
		$l_allocatedUser=$this->getAllocatedUser_id_Ref();
		return $this->getCreatingUser_id()==$p_userId
			|| $this->getAllocatedUser_id()==$p_userId
			|| (isset($l_allocatedUser) && $this->getAllocatedUser_id_Ref()->isUserManager($p_userId));
	}
	
	/**
	 * Extension of the base data class.
	 * Return the label of the item type
	 * @return string a Label
	 */
	public function getItemTypeLabel(){
		return $this->getItemType_id_Ref()->getLabel();
	}
	
	/**
	 * Extension of the base data class.
	 * Return the label of the item status
	 * @return string a Label
	 */
	public function getItemStatusLabel(){
		return $this->getItemStatus_id_Ref()->getLabel();
	}
	
	/**
	 * Extension of the base data class.
	 * Return the title of the related activity.
	 * @return string a Label
	 */
	public function getActivityLabel(){
		return $this->getActivity_id_Ref()->getTitle();
	}

	/**
	 * Return true when the Item has a TargetDate in the past
	 * @return boolean
	 */
	public function getOverdue(){
		return (strtotime($this->getTargetDate())-time())<0;
	}
	
	/**
	 * Return an HTML flag to be displayed when the
	 * Item has a TargetDate in the past
	 */
	public function getOverdueHtmlFlag(){
		if($this->getOverdue()){
			return "<img width= \"30\" height=\"30\" src=\"" . ROOT_PATH . "/apps/resources/images/critical.png\"/>";
		}
		return "";
	}
	
	/**
	 * Return the number of comments associated with the specified item
	 * @return integer a number
	 */
	public function getCommentCount(){
		return Comment::getCustomCount("WHERE Item_Id=:Item_Id", array("Item_Id" => $this->getId()));
	}
	
	/**
	 * Return the full name of the user who created the item
	 * @return string a full name
	 */
	public function getCreatingUserFullName(){
		return $this->getCreatingUser_id_Ref()->getFullName();
	}
}
?>